const App = getApp()

Page({
    data:{
        navbar:['全部','待付款','待发货','待收货','已完成','退款订单'],
        currentTab:0,
        orderList:[]
    },
    onLoad(option){
        this.setData({ 
        currentTab: option. type
        }) 
         this.getData(option.type)
         if(option.please =='pay'){
             this.payOrderMoney(option.id)
         }
    },
    onShow:function(){
         this.getData(this.data.currentTab)
    },
    navbarTap: function(e){ 
        this.setData({ 
        currentTab: e.currentTarget.dataset.idx 
        })
        this.getData(e.currentTarget.dataset.idx)
    
    } ,
    getData:function(status){
        var e=Number(status)
          switch(e) {
            case  0: this.getAllOrder(0); break; //获取全部订单

            case  1: this.getAllOrder(1);break; //获取待支付的订单

            case  2: this.getAllOrder(2);break;//获取待发货的订单

            case  3: this.getAllOrder(3);break;//获取待收货的订单

            case  4: this.getAllOrder(4);break;//获取已完成的订单

            case  5: this.getAllOrder(6);break;//获取申请退款的订单

            default :break;
        }
    },
    getAllOrder:function(status){
        //获取全部订单
        var that=this
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration: 20000
        })
        App.HttpService.getOrdersData({
            user_id:wx.getStorageSync('user_id'),
            status:status
        }).then(data=>{
            if(data.meat.code==0){
                wx.hideToast()
                that.setData({
                    orderList:data.data
                })
                
            }
        })
    },

    //点击查看订单详情
    clickDetail:function(e){
        var id=e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '../order/detail/index?id='+id,
        })
    },
    //取消订单
    removeOrder:function(e){
        var id=e.currentTarget.dataset.id
        var that=this
        wx.showModal({
            title: '温馨提示',
            content: '是否取消该订单?',
            success: function(res) {
                if (res.confirm) {
                 wx.showToast({
                    title: '取消中...',
                    icon: 'loading',
                    duration: 20000
                    })
                App.HttpService.removeOrders({
                    id:id
                }).then(data=>{
                    wx.hideToast()
                    if(data.meat.code == 0){
                        wx.showToast({
                            title:'取消成功',
                            icon:'success',  
                        })
                        setTimeout(function(){
                             that.getData(that.data.currentTab)
                        },1500)
                       
                    }
                })     
                }
            }
        })
       
    },
        //联系客服
    callCustomService:function(){
    
        wx.makePhoneCall({
          phoneNumber: '18079137975',
        })
    },
    //确定收货
    confrimTake:function(e){
        var id=e.currentTarget.dataset.id
        var that=this
         wx.showModal({
            title: '温馨提示',
            content: '是否确定收货?',
            success: function(res) {
                if (res.confirm) {
                 wx.showToast({
                    title: '操作中...',
                    icon: 'loading',
                    duration: 20000
                    })
                App.HttpService.confirmTake({
                    id:id
                }).then(data=>{
                    wx.hideToast()
                    if(data.meat.code == 0){
                        wx.showToast({
                            title:'收货成功',
                            icon:'success',  
                        })
                        setTimeout(function(){
                             that.getData(that.data.currentTab)
                        },1500)
                       
                    }
                })     
                }
            }
        })
    },
    //微信支付

    payOrders:function(e){
       
         var that=this
         var id=e.currentTarget.dataset.id
        wx.showToast({
            title: '请稍后..',
            icon: 'loading',
            duration: 200000
        })
        wx.login({
        success: function(res){
            if(res.code){
            App.HttpService.getUserOpenid({
            code:res.code,
            id:id
            }).then(data=>{
               var success=("appId" in data)
                if(success==false){
                     wx.showToast({
                        title:data.data,
                        icon:'loading',
                        image:'../../assets/images/error-icon.png',
                    })
                }else{
                wx.hideToast()
                wx.requestPayment({
                    'timeStamp': data.timeStamp,
                    'nonceStr': data.nonceStr,
                    'package': data.package,
                    'signType': 'MD5',
                    'paySign': data.paySign,
                    success: function(res){
                        wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                            duration: 2000
                        })
                        setTimeout(function(){
                            App.HttpService.editOrderStatus({
                                id:id,
                                status:1
                            }).then(data=>{
                                if(data.meat.code == 0){
                                    that.getData(that.data.currentTab)
                                }
                            })
                        },2000)
                    },
                    
                    "fail":function(res){
                        that.getData(that.data.currentTab)
                         wx.showToast({
                            title: '支付失败',
                            icon: 'loading',
                            image:'../../assets/images/error-icon.png',
                            duration: 2000
                        })
                    }
                })
                }
             
            })
            }else{
            wx.showToast({
                title:'获取用户信息失败',
                icon:'loading',
                image:'../../assets/images/error.png',
            })
            that.getData(that.data.currentTab)
            }
        },
        })
    },

    //payOrderMoney
    payOrderMoney:function(id){
          var that=this
        wx.showToast({
            title: '请稍后..',
            icon: 'loading',
            duration: 200000
        })
        wx.login({
        success: function(res){
            if(res.code){
                App.HttpService.getUserOpenid({
                code:res.code,
                id:id
                }).then(data=>{
                    var success=("appId" in data)
                    if(success==false){
                        wx.showToast({
                            title:data.data,
                            icon:'loading',
                            image:'../../assets/images/error-icon.png',
                        })
                    }else{
                        wx.hideToast()
                        wx.requestPayment({
                            'timeStamp': data.timeStamp,
                            'nonceStr': data.nonceStr,
                            'package': data.package,
                            'signType': 'MD5',
                            'paySign': data.paySign,
                            success: function(res){
                                wx.showToast({
                                    title: '支付成功',
                                    icon: 'success',
                                    duration: 2000
                                })
                                setTimeout(function(){
                                    App.HttpService.editOrderStatus({
                                        id:id,
                                        status:1
                                    }).then(data=>{
                                        if(data.meat.code == 0){
                                            that.getData(that.data.currentTab)
                                        }
                                    })
                                },2000)
                            },
                            "fail":function(res){
                                that.getData(that.data.currentTab)
                                wx.showToast({
                                    title: '支付失败',
                                    icon: 'loading',
                                    image:'../../assets/images/error-icon.png',
                                    duration: 2000
                                })
                                
                            }
                        })
                    }
                
                })
            }else{
            wx.showToast({
                title:'获取用户信息失败',
                icon:'loading',
                image:'../../assets/images/error-icon.png',
            })
            }
        },
        })
    },

    //申请退款
    applyRefund:function(e){
        var id=e.target.dataset.id
        var that=this
         wx.showToast({
                title:'申请退款中...',
                icon:'loading', 
                duration: 20000
        })
        App.HttpService.applyRefund({
            id:id
        }).then(data=>{
             wx.hideToast()
             if(data.meat.code == 0){
                 wx.showToast({
                            title:'申请成功',
                            icon:'success',  
                        })
                setTimeout(function(){
                     that.getData(that.data.currentTab)
             },1500)
             }
        })
    }
})